<?php

declare(strict_types=1);

namespace App\AdminApi\Modular\Administer\Controller;

use App\AdminApi\BasicsController;
use App\AdminApi\Modular\Administer\Validate\RoleValidate;
use App\Common\Extend\Guard\Auth;
use App\Common\Modular\Administer\Serve\RoleServe;
use Core\HyperfUnit\Response;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\{Controller, GetMapping, PostMapping};
use Psr\Http\Message\ResponseInterface;
use Zms\HyperfValidateAnnotations\Validate;

#[
    Controller(prefix: "/admin-api/administer-role", server: self::SERVER),
    Auth(guard: self::GUARD, label: ['后台管理', '角色'], app: self::APP)
]
class RoleController extends BasicsController
{
    #[Inject]
    protected RoleServe $service;

    #[
        GetMapping('list'),
        Auth(sign: 'administer.role.list', view: '/administer/role/index'),
    ]
    public function list(): ResponseInterface
    {
        $where = $this->request->inputs(['id', 'name', 'status']);
        $paginate = $this->service->search($where)->orderByRaw($this->orderBy('id DESC'))->paginate($this->pageSize());
        return Response::Api()->success(data: $paginate);
    }


    #[
        PostMapping('created'),
        Auth(sign: 'administer.role.created', logger: "POST"),
        Validate(class: RoleValidate::class, scene: "created")
    ]
    public function created(): ResponseInterface
    {
        $data = $this->request->getParsedBody();
        if ($this->service->created($data)) {
            return Response::Api()->success('角色添加成功');
        }
        return Response::Api()->fail('角色添加失败', $data);
    }


    #[
        PostMapping('updated'),
        Auth(sign: 'administer.role.updated', logger: "POST"),
        Validate(class: RoleValidate::class, scene: "updated")
    ]
    public function updated(): ResponseInterface
    {
        $data = $this->request->getParsedBody();
        if ($this->service->updated($data)) {
            return Response::Api()->success('角色编辑成功');
        }
        return Response::Api()->fail('角色编辑失败', $data);
    }


    #[
        PostMapping('status'),
        Auth(relyon: ["administer.role.updated"], logger: false, describe: '修改角色状态'),
        Validate(rules: \Core\Constants\Validate::CHANGE_STATUS_RULES)
    ]
    public function status(): ResponseInterface
    {
        $data = $this->request->inputs(['id', 'status']);
        if ($this->service->search(['id' => $data['id']])->update(['status' => $data['status']])) {
            return Response::Api()->success('更改成功');
        }
        return Response::Api()->fail('更改失败');
    }


    #[
        PostMapping('remove'),
        Auth(sign: 'administer.role.remove')
    ]
    public function remove(): ResponseInterface
    {
        $id = $this->request->input('id', '0');
        $role = $this->service->search(['id' => $id])->delete();
        if ($role) {
            return Response::Api()->success('角色已删除');
        }
        return Response::Api()->fail('角色删除失败');
    }


    #[
        GetMapping('details'),
        Auth(label: ['角色详情'], relyon: ['administer.role.list', 'administer.role.created', 'administer.role.updated'], logger: false)
    ]
    public function details(): ResponseInterface
    {
        $id = $this->request->input('id', '0');
        $role = $this->service->search(['id' => $id])->first();
        if ($role) {
            return Response::Api()->success(data: $role->toArray());
        }
        return Response::Api()->fail('角色不存在或已删除');
    }
}